package com.uchengwang.ui.cm_ui.uu.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.ucity.listener.RxBusResultSubscriber;
import com.uchengwang.ui.listener.PermissionsListener;
import com.uchengwang.ui.utils.PermissionUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.QRCodeDecoder;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageRadioResultEvent;

/**
 * Created by U城网 on 2018/1/31.
 * 二维码扫描界面
 */

public class QRScanActivity extends BaseWithTitleActivity implements QRCodeView.Delegate{
    private QRCodeView qrCodeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_qr_scan);
        setTitle("扫一扫");
        setRightText("相册", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UCommonUtil.getUtil().openGallerySingle(QRScanActivity.this, new RxBusResultSubscriber<ImageRadioResultEvent>() {
                    @Override
                    public void onEvent(ImageRadioResultEvent event) {
                        Log.d("jjf", "地址=="+event.getResult().getOriginalPath());
                        String url = event.getResult().getOriginalPath();
                        String result = QRCodeDecoder.syncDecodeQRCode(url);

                        if (!TextUtils.isEmpty(result)){
                            resolver(result);
                        }else {
                            UCommonUtil.getUtil().showToast(QRScanActivity.this, "没有识别到二维码信息");
                        }
                    }
                });
            }
        });
        initView();

    }

    private void initView(){
        qrCodeView = (ZXingView) findViewById(R.id.scan_xingView);
        qrCodeView.setDelegate(this);

        checkPermission();
    }

    //检查权限
    private void checkPermission(){
        PermissionUtils.getUtils().requestPermissions(this, "请授权", new PermissionsListener() {
            @Override
            public void onSuccess() {
                load();
            }

            @Override
            public void onFailed() {
                Toast.makeText(QRScanActivity.this, "请授权", Toast.LENGTH_SHORT).show();
            }
        }, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    //启动扫描
    private void load(){
        qrCodeView.startCamera();
        qrCodeView.showScanRect();
        qrCodeView.startSpot();
    }

    //扫描成功  震动提示
    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    //扫描成功 回调方法
    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
//        qrCodeView.startSpot();//扫描成功 还需在此调用此方法 才能再次扫描
        resolver(result);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        UCommonUtil.getUtil().showToast(this, "打开相机出错");
    }

    /**
     * 识别二维码信息
     * */
    private void resolver(String codeInfo){
        if (!TextUtils.isEmpty(codeInfo)){
            Intent intent = new Intent(this, LoadCycleWebHtmlActivity.class);
            intent.putExtra(BaseConfig.URL, codeInfo);
            startActivity(intent);


//            if (codeInfo.contains("person")){
//                //跳转到用户界面
//                int index = codeInfo.indexOf("?");
//                codeInfo = codeInfo.substring(index);
//                String[] s = codeInfo.split("=");
//
////                Intent intent = new Intent(this, MainPageActivity.class);
////                intent.putExtra(BaseConfig.ID, Long.parseLong(s[1]));
////                startActivity(intent);
//                Intent intent = new Intent(this, LoadCycleWebHtmlActivity.class);
//                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+s[1]);
//                startActivity(intent);
//            }
            finish();
        }else {
            UCommonUtil.getUtil().showToast(this, "识别错误");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        load();
    }

    @Override
    protected void onStop() {
        qrCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        qrCodeView.onDestroy();
        super.onDestroy();
    }
}
